package com.zhongfl.crm.api.view;

import com.zhongfl.crm.model.vo.LxRevisitLogVO;
import com.zhongfl.crm.service.LxRevisitLogServiceCRM;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tech.heptagram.core.common.rest.ResponseResult;

import javax.annotation.Resource;
import java.util.*;

/**
 * 跟进记录controller
 *
 * @author tiantian
 * @date 2018/9/25 15:20
 */
@RestController
@RequestMapping("/lxRevisitLog")
public class LxRevisitLogController {

    @Resource
    private LxRevisitLogServiceCRM lxRevisitLogServiceCRM;

    @GetMapping("/query")
    public ResponseResult<Map<String, List<LxRevisitLogVO>>> findProductList(@RequestParam Long loggableId, @RequestParam String loggableType) {
        List<LxRevisitLogVO> lxRevisitLogVOs = lxRevisitLogServiceCRM.findByLoggableIdAndLoggableType(loggableId, loggableType);
        Map<String, List<LxRevisitLogVO>> resultMap = this.groupByDay(lxRevisitLogVOs);
        return new ResponseResult<>(resultMap);
    }

    private Map<String, List<LxRevisitLogVO>> groupByDay(List<LxRevisitLogVO> lxRevisitLogVOs) {
        if (CollectionUtils.isEmpty(lxRevisitLogVOs)) {
            return new HashMap<>();
        }

        Map<String, List<LxRevisitLogVO>> result = new LinkedHashMap<>();

        for (LxRevisitLogVO vo : lxRevisitLogVOs) {
            String key = vo.getDay();
            if (result.containsKey(key)) {
                result.get(key).add(vo);
            } else {
                result.put(key, new ArrayList<>());
                result.get(key).add(vo);
            }
        }

        return result;
    }
}
